// pages/mine/mine.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:wx.getStorageSync('userInfo'),
    userlist:[],
    coupon:0,
    storagevalue:0,
    accumulatepoints:0,
    userName:"点击授权",
    isdisabled:false,
    memberlevel:"铜牌会员",
    userSex:"",
    userNumber:"",
    userBirthday:"",
    
  },  
binddetail(ev){
  console.log(ev)
  wx.navigateTo({
    //url: '../mydetail/mydetail'
    url: '../mydetail/mydetail?userInfo='+ev.currentTarget.dataset.userInfo+'&nickName='+ev.currentTarget.dataset.userinfo.nickName
  })
},
bindmyorder(ev){
  wx.navigateTo({
    url: '../myorder/myorder',
  })
},
bindstoragevalue(ev){
  wx.navigateTo({
    url: '../mystoragevalue/mystoragevalue',
  })
},
bindlevel(){
  wx.navigateTo({
    url: '../viplevel/viplevel',
  })
},
bindmembercode(){
  wx.navigateTo({
    url: '../mycode/mycode',
  })
},
bindcoupon(){
  wx.navigateTo({
    url: '../cheap/cheap',
  })
},
bindaccumulatepoints(){
  wx.navigateTo({
    url: '../mypoint/mypoint',
  })
},
bindmall(){
  wx.navigateTo({
    url: '../point/point',
  })
},
bindexchangecode(){
  wx.navigateTo({
    url: '../exchange/exchange',
  })
},
bindnotice(){
  wx.navigateTo({
    url: '../notice/notice',
  })
},
handleUserInfo(ev){
    console.log(ev)
    this.setData({
      userInfo:ev.detail.userInfo,
      isdisabled:(!this.isdisabled),
    })
    wx.setStorageSync('userInfo', ev.detail.userInfo)
    wx.request({
    url:`http://localhost:3000/users?nickName=${ev.detail.userInfo.nickName}`,
    success:(re)=>{
      if(re.data.length===0){  //判断用户是否已经存在
        wx.request({
          url:'http://localhost:3000/users',
          method:"post",
          data:ev.detail.userInfo,
          success:()=>{
            wx.showToast({
              title:'登陆成功'
            })
          }
        })
      }
      else{
          this.setData({
              coupon:re.data[0].property[0].coupon,  //取得用户信息
              storagevalue:re.data[0].property[0].storagevalue,
              accumulatepoints:re.data[0].property[0].accumulatepoints,
              userNumber:re.data[0].phonenumber,
              userName:re.data[0].nickName,
              userBirthday:re.data[0].birthday
            })  
        if(re.data[0].property[0].accumulatepoints<1000&&re.data[0].property[0].accumulatepoints>400){
          this.setData({
            memberlevel:"银牌会员"
           })
        }
        if(re.data[0].property[0].accumulatepoints<1800&&re.data[0].property[0].accumulatepoints>1000){
            this.setData({
              memberlevel:"白银会员"
            })
        }
        if(re.data[0].property[0].accumulatepoints<2800&&re.data[0].property[0].accumulatepoints>1800){
            this.setData({
              memberlevel:"黄金会员"
            })
        }
        if(re.data[0].property[0].accumulatepoints<4200&&re.data[0].property[0].accumulatepoints>2800){
            this.setData({
              memberlevel:"铂金会员"
            })
        }
        if(re.data[0].property[0].accumulatepoints>=4200){
            this.setData({
              memberlevel:"钻石会员"
            })
        }
        if (re.data[0].gender===1) {
            this.setData({
                userSex:"男"
            })    
        }
        else{
            this.setData({
              userSex:"女"
          })  
        }
      wx.showToast({
        title:'登陆成功'
      })
      }
    }
    })
},
   
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.login({
      success: function (res) {
          if (res.code) {
              //发起网络请求
              console.log(res.code)
          } else {
              console.log('获取用户登录态失败！' + res.errMsg)
          }
      }
     });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

}
})
